const { ipcRenderer } = require('electron');

// 获取客户端表格的 tbody 元素
const clientTableBody = document.getElementById('client-table').getElementsByTagName('tbody')[0];

// 监听客户端连接事件
ipcRenderer.on('client-connected', (event, { clientId }) => {
  // 为每个客户端动态创建一行表格
  const row = document.createElement('tr');

  // 添加客户端 ID 列
  const clientIdCell = document.createElement('td');
  clientIdCell.textContent = clientId;
  row.appendChild(clientIdCell);

  // 添加日期和时间范围选择列
  const dateRangeCell = document.createElement('td');
  const dateInput = document.createElement('input');
  dateInput.type = 'date';
  const startTimeInput = document.createElement('input');
  startTimeInput.type = 'time';
  const endTimeInput = document.createElement('input');
  endTimeInput.type = 'time';
  dateRangeCell.appendChild(dateInput);
  dateRangeCell.appendChild(startTimeInput);
  dateRangeCell.appendChild(endTimeInput);
  row.appendChild(dateRangeCell);

  // 添加视频描述和文件夹输入框列
  const inputCell1 = document.createElement('td');
  const input1 = document.createElement('input');
  input1.type = 'text';
  inputCell1.appendChild(input1);
  row.appendChild(inputCell1);

  const inputCell2 = document.createElement('td');
  const input2 = document.createElement('input');
  input2.type = 'text';
  inputCell2.appendChild(input2);
  row.appendChild(inputCell2);

  // 添加地址输入框列
  const addressCell = document.createElement('td');
  const addressInput = document.createElement('input');
  addressInput.type = 'text';
  addressCell.appendChild(addressInput);
  row.appendChild(addressCell);

  // 添加发布数量输入框列
  const quantityCell = document.createElement('td');
  const quantityInput = document.createElement('input');
  quantityInput.type = 'number';
  quantityInput.placeholder = '发布数量';
  quantityCell.appendChild(quantityInput);
  row.appendChild(quantityCell);

  // 添加发布间隔输入框列
  const intervalCell = document.createElement('td');
  const intervalInput = document.createElement('input');
  intervalInput.type = 'number';
  intervalInput.placeholder = '发布间隔（分钟）';
  intervalCell.appendChild(intervalInput);
  row.appendChild(intervalCell);

  // 添加状态列
  const statusCell = document.createElement('td');
  statusCell.textContent = '等待任务'; // 默认状态
  row.appendChild(statusCell);

  // 监听 WebSocket 消息更新状态
  const updateStatus = (statusMessage) => {
    statusCell.textContent = statusMessage;
  };

  // 获取到任务开始或任务完成等状态时可以通过 ipcRenderer 发消息更新状态
  ipcRenderer.on(`task-status-${clientId}`, (event, statusMessage) => {
    updateStatus(statusMessage);
  });

  // 获取消息输入框
  const messageInput = document.getElementById('massage-input');

  // 添加开始任务按钮列
  const buttonCell = document.createElement('td');
  const startTaskButton = document.createElement('button');
  startTaskButton.textContent = '开始任务';
  startTaskButton.addEventListener('click', () => {
    const date = dateInput.value;
    const startTime = startTimeInput.value;
    const endTime = endTimeInput.value;
    const description = input1.value;
    const folder = input2.value;
    const address = addressInput.value;  // 获取地址输入框的值
    const message = messageInput.value;
    const quantity = quantityInput.value;  // 获取发布数量
    const interval = intervalInput.value;  // 获取发布间隔
    
    console.log(`Starting task for ${clientId} on ${date} from ${startTime} to ${endTime}, topic: ${description}, folder: ${folder}, address: ${address}, quantity: ${quantity}, interval: ${interval}`);
    
    // 向主进程发送开始任务的请求，包括 quantity 和 interval 信息
    ipcRenderer.send('start-task', {
      clientId,
      taskDetails: { date, startTime, endTime, description, folder, address, message, quantity, interval }
    });

    // 更新状态为 "任务开始"
    updateStatus('任务开始');
  });
  buttonCell.appendChild(startTaskButton);
  row.appendChild(buttonCell);

  // 将客户端行添加到表格中
  clientTableBody.appendChild(row);
});

// 监听客户端消息事件
ipcRenderer.on('client-message', (event, { clientId, message }) => {
  console.log(`Message from ${clientId}:`, message);
});

// 监听客户端断开连接事件
ipcRenderer.on('client-disconnected', (event, { clientId }) => {
  const rows = clientTableBody.getElementsByTagName('tr');
  for (let row of rows) {
    const clientIdCell = row.getElementsByTagName('td')[0];
    if (clientIdCell && clientIdCell.textContent === clientId) {
      clientTableBody.removeChild(row);
      break;
    }
  }
});
